require './HashTags.rb'

class Observations

    OBS_TABLE             = "observations"
    OBS_TABLE_ID          = "id"
    OBS_TABLE_TAG_ID      = "tag_id"
    OBS_TABLE_TOD         = "time_of_day"
    OBS_TABLE_DOW         = "day_of_week"
    OBS_TABLE_TWEET_ID    = "tweet_id"
    OBS_TABLE_TEXT        = "text"
    OBS_TABLE_DELETED     = "deleted"

    def initialize(mysql_con, hashtag)
        @mysql_connection, @hashtag     = mysql_con, hashtag
        @observations                   = Array.new()
    end

    def load()
        @observations.clear()
        query_string    = "SELECT * FROM #{OBS_TABLE} WHERE #{OBS_TABLE_TAG_ID} = #{@hashtag.id} AND #{OBS_TABLE_DELETED} = 0"
        results         = @mysql_connection.query(query_string);
        results.each_hash do |row| 
            @observations.push(Tweet.new(row[OBS_TABLE_ID],
                                         row[OBS_TABLE_TAG_ID],
                                         row[OBS_TABLE_TOD],
                                         row[OBS_TABLE_DOW],
                                         row[OBS_TABLE_TWEET_ID],
                                         row[OBS_TABLE_TEXT]))
        end
    end

    def update()
        last_id = 0
        @observations.each do |tw| 
            if (last_id < tw.id)
                last_id = tw.id;
            end
        end
        query_string    = "SELECT * FROM #{OBS_TABLE} WHERE #{OBS_TABLE_TAG_ID} = #{@hashtag.id} AND #{OBS_TABLE_ID} > #{last_id}"
        results         = @mysql_connection.query(query_string);
        results.each_hash do |row|
            @observations.push(Tweet.new(row[OBS_TABLE_ID],
                                         row[OBS_TABLE_TAG_ID],
                                         row[OBS_TABLE_TOD],
                                         row[OBS_TABLE_DOW],
                                         row[OBS_TABLE_TWEET_ID],
                                         row[OBS_TABLE_TEXT]))
        end
        query_string    = "SELECT #{OBS_TABLE_ID} FROM #{OBS_TABLE} WHERE #{OBS_TABLE_TAG_ID} = #{@hashtag.id} AND #{OBS_TABLE_DELETED} = 1"
        results         = @mysql_connection.query(query_string);
        results.each_hash do |row|
            @observations.delete_if { |tw| tw.id == row[OBS_TABLE_ID] }
        end
    end

    def save()
        query_string     = "INSERT INTO #{OBS_TABLE} (#{OBS_TABLE_TAG_ID}, #{OBS_TABLE_TOD}, #{OBS_TABLE_DOW}, #{OBS_TABLE_TWEET_ID}, #{OBS_TABLE_TEXT}) VALUES "
        new_obs = @observations.select { |tw| tw.state == :added }
        if (!new_obs.empty?)
            new_obs.each do |tw|
                query_string += "( #{tw.tag_id}, #{tw.time_of_day}, #{tw.day_of_week}, #{tw.tweet_id}, '#{tw.text}' ),"
            end
            query_string = query_string[0..-2] + ";"
            @mysql_connection.query(query_string);
        end

        del_obs = @observations.select { |tw| tw.state == :removed }
        query_string     = "DELETE FROM #{OBS_TABLE} WHERE #{OBS_TABLE_ID} IN ( "
        if (!del_obs.empty?)
            del_obs.each { |tw| query_string += " #{tw.id}," }
            query_string += query_string[0..-2] + ");"
            @mysql_connection.query(query_string);
        end
    end

    def add_observation(tod, dow, tweet_id, text)
        if (@observations.count { |tw| tw.tweet_id == tweet_id } == 0)
            @observations.push(Tweet.new(Tweet::OBS_NULL_ID, @hashtag.id, tod, dow, tweet_id, text, :added)
        end
    end

    def remove_observation(id)
        del_obs         = @observations.select { |tw| tw.id == id }
        if (!del_obs.empty?)
            obs         = del_obs.first()
            @observations.delete_if { |tw| tw.id == id }
            old_obs     = Tweet.new(obs.id, obs.tag_id, obs.time_of_day, obs.day_of_week, obs.tweet_id, obs.text, :removed)
            @observations.push(old_obs)
        end
    end

    def get_observations()
        return @observations.clone()
    end
end


class Tweet
    
    OBS_NULL_ID = -1

    attr_reader :id, :tag_id, :time_of_day, :day_of_week, :tweet_id, :text, :state
    
    def initialize(obs_id, hashtag_id, tod, dow, obs_tweet_id, obs_text, obs_state)
        @id, @tag_id, @time_of_day, @day_of_week    = obs_id, hashtag_id, tod, dow
        @tweet_id, @text, @state                    = obs_tweet_id, obs_text, obs_state
    end

end
